<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\erp\home;

use app\common\controller\AuthCheck;

/**
 * 商品管理
 * @package app\erp\home
 */
class Goods extends AuthCheck
{
    // 定义全局的查询范围
    protected function base($query)
    {
        $query->where('goods_status', 0);
    }

    /**
     * 商品列表
     * @author jason
     * @return mixed
     */
    public function goods_list(){
		//参数接收
        $params = $this->getParams(0);//0 get 1 post 
        $user_id    = intval(isset($params["user_id"]) ? $params["user_id"] : 0);
        
        //数据验证
        $Validate = validate('Goods', 'validate');
        if( !$Validate -> scene('goods_list') -> check($params) ){
            json_out(-1001, $Validate -> getError(), (object)[]);//验证失败
        }

        //逻辑处理
        $list = controller('Goodslogic', 'logic') -> goods_list($user_id);

        //数据返回
        json_out($list['code'], $list['message'], $list['data']);        
    }

    /**
     * 商品列表
     * @author jason
     * @return mixed
     */
    public function goods_info(){
		//参数接收
        $params = $this->getParams(0);//0 get 1 post 
        $goods_id    = intval(isset($params["goods_id"]) ? $params["goods_id"] : 0);
        $store    = intval(isset($params["store"]) ? $params["store"] : 0);
        //数据验证
        $Validate = validate('Goods', 'validate');
        if( !$Validate -> scene('goods_info') -> check($params) ){
            json_out(-1001, $Validate -> getError(), (object)[]);//验证失败
        }

        //逻辑处理
        $list = controller('Goodslogic', 'logic') -> getGoodsInfo($goods_id, $store);

        //数据返回
        json_out($list['code'], $list['message'], $list['data']);        
    }

    

    /**
     * 商品列表
     * @author jason
     * @return mixed
     */
    public function goods_add(){
		//参数接收
        $params = $this->getParams(1);//0 get 1 post 
        //测试参数
        /* $params['parameter'] = [ //参数
            [
                'parameter_id' => 1,
                'attribute_id' => 7,
            ],
            [
                'parameter_id' => 3,
                'attribute_id' => 13,
            ],
        ];
        $params['specifications'] = [ //规格
            [
                'specifications_id' => 1,
                'specifications_title' => '尺寸',
                'attribute'             => [
                    [
                        'attribute_id'      => 1,
                        'attribute_title'   => 'M',
                    ],
                    [
                        'attribute_id'      => 2,
                        'attribute_title'   => 'L',
                    ],
                ],
            ],
            [
                'specifications_id' => 2,
                'specifications_title' => '颜色',
                'attribute'             => [
                    [
                        'attribute_id'      => 1,
                        'attribute_title'   => '红',
                    ],
                    [
                        'attribute_id'      => 2,
                        'attribute_title'   => '白',
                    ],
                ],
            ],
        ];

        $params['single_item'] = [//单品信息
            [

                'goods_specifications'          => 'M 红',
                //'specifications1_id'         => 1,
                //'specifications1'            => '规格名称(尺寸)',
                'specifications1_attribute_id'  => 3,
                'specifications1_attribute'  => '属性命名 M',
                //'specifications2_id'         => 7,
                'specifications2_attribute_id'  => 8,
                //'specifications2'            => '规格名称(颜色)',
                'specifications2_attribute'  => '红',
                'article_no'                => '国际条形码',
                'weight'                    => '重量',
                'volume'                    => '体积',
                'price'                     => 10.01,
                'sku_status'                => 1,
            ],
            [
                'goods_specifications'          => 'M 白',
                //'specifications1_id'         => 1,
                //'specifications1'            => '规格名称(尺寸)',
                'specifications1_attribute_id'  => 4,
                'specifications1_attribute'  => '属性命名 M',
                //'specifications2_id'         => 6,
                'specifications2_attribute_id'  => 9,
                //'specifications2'            => '规格名称(颜色)',
                'specifications2_attribute'  => '白',
                'article_no'                => '国际条形码',
                'weight'                    => '重量',
                'volume'                    => '体积',
                'price'                     => 10.01,
                'sku_status'                => 0,
            ],
        ];

        $params['cover_video_arr'] = [1,2,3];//封面图片id
        $params['goods_classid']   = [1,2,3];//分类id */
        //$params['details_arr'] = [1,2,3];//封面图片id
        //测试参数

        $user_id                = intval(isset($params["user_id"]) ? $params["user_id"] : 0);
        $supplier_id            = intval(isset($params["supplier_id"]) ? $params["supplier_id"] : 0);//所属供应商
        $goods_name             = strval(isset($params["goods_name"]) ? $params["goods_name"] : '');//商品名称
        $goods_classid          = isset($params["goods_classid"]) ? $params["goods_classid"] : [];//分类id（数组）
        $goods_classtempid            = intval(isset($params["goods_classtempid"]) ? $params["goods_classtempid"] : 0);//所属供应商
        $goods_code             = strval(isset($params["goods_code"]) ? $params["goods_code"] : '');//商品编码
        $cover_video_arr         = isset($params["cover_video_arr"]) ? $params["cover_video_arr"] : [];//封面缩略图（数组）
        $brand_id               = isset($params["brand_id"]) ? $params["brand_id"] : 0;//品牌id
        $unit_measurement       = strval(isset($params["unit_measurement"]) ? $params["unit_measurement"] : '');//计量单位
        $sort                   = intval(isset($params["sort"]) ? $params["sort"] : 100);//排序编号
        $parameter              = isset($params["parameter"]) ? $params["parameter"] : [];//商品参数
        $specifications              = isset($params["specifications"]) ? $params["specifications"] : [];//商品规格

        $single_item            = isset($params["single_item"]) ? $params["single_item"] : [];//单品数组

        $is_single_specification  = isset($params["is_single_specification"]) ? $params["is_single_specification"] : 0;//是否多规格 0否 1是
        $details_arr         = isset($params["details_arr"]) ? $params["details_arr"] : [];//商品详情图
        
        
        //数据验证
        $Validate = validate('Goods', 'validate');
        if( !$Validate -> scene('goods_add') -> check($params) ){
            json_out(-1001, $Validate -> getError(), (object)[]);//验证失败
        }
        
        //逻辑处理
        $list = controller('Goodslogic', 'logic') 
        -> goods_add($user_id, $goods_name, $goods_code, $brand_id, $supplier_id, $goods_classid, $cover_video_arr, $unit_measurement, $parameter, $single_item, $is_single_specification, $specifications, $details_arr, $goods_classtempid);

        //数据返回
        json_out($list['code'], $list['message'], (object)$list['data']);        
    }

    /**
     * 商品 编辑
     * @author jason
     * @return mixed
     */
    public function goods_edit(){
		//参数接收
        $params = $this->getParams(1);//0 get 1 post 
        //测试参数
         /* $params['parameter'] = [ //参数
            [
                'parameter_id' => 1,
                'attribute_id' => 7,
            ],
            [
                'parameter_id' => 3,
                'attribute_id' => 13,
            ],
        ];
        $params['specifications'] = [ //规格
            [
                'specifications_id' => 1,
                'specifications_title' => '尺寸',
                'attribute'             => [
                    [
                        'attribute_id'      => 1,
                        'attribute_title'   => 'M',
                    ],
                    [
                        'attribute_id'      => 2,
                        'attribute_title'   => 'L',
                    ],
                ],
            ],
            [
                'specifications_id' => 2,
                'specifications_title' => '颜色',
                'attribute'             => [
                    [
                        'attribute_id'      => 1,
                        'attribute_title'   => '红',
                    ],
                    [
                        'attribute_id'      => 2,
                        'attribute_title'   => '白',
                    ],
                ],
            ],
        ];

        $params['single_item'] = [//单品信息
            [

                'goods_specifications'          => 'M 红',
                //'specifications1_id'         => 1,
                //'specifications1'            => '规格名称(尺寸)',
                'specifications1_attribute_id'  => 3,
                'specifications1_attribute'  => '属性命名 M',
                //'specifications2_id'         => 7,
                'specifications2_attribute_id'  => 8,
                //'specifications2'            => '规格名称(颜色)',
                'specifications2_attribute'  => '红',
                'article_no'                => '国际条形码',
                'weight'                    => '重量',
                'volume'                    => '体积',
                'price'                     => 10.01,
                'sku_status'                => 1,
            ],
            [
                'goods_specifications'          => 'M 白',
                //'specifications1_id'         => 1,
                //'specifications1'            => '规格名称(尺寸)',
                'specifications1_attribute_id'  => 4,
                'specifications1_attribute'  => '属性命名 M',
                //'specifications2_id'         => 6,
                'specifications2_attribute_id'  => 9,
                //'specifications2'            => '规格名称(颜色)',
                'specifications2_attribute'  => '白',
                'article_no'                => '国际条形码',
                'weight'                    => '重量',
                'volume'                    => '体积',
                'price'                     => 10.01,
                'sku_status'                => 0,
            ],
        ];

        $params['cover_video_arr'] = [1,2,3];//封面图片id
        $params['goods_classid']   = [1,2,3];//分类id
        $params['details_arr'] = [1,2,3];//封面图片id */
        //测试参数

        $user_id                = intval(isset($params["user_id"]) ? $params["user_id"] : 0);
        $goods_id                = intval(isset($params["goods_id"]) ? $params["goods_id"] : 0);
        $supplier_id            = intval(isset($params["supplier_id"]) ? $params["supplier_id"] : 0);//所属供应商
        $goods_name             = strval(isset($params["goods_name"]) ? $params["goods_name"] : '');//商品名称
        $goods_classid          = isset($params["goods_classid"]) ? $params["goods_classid"] : [];//分类id（数组）
        $goods_classtempid      = intval(isset($params["goods_classtempid"]) ? $params["goods_classtempid"] : 0);//所属供应商
        $goods_code             = strval(isset($params["goods_code"]) ? $params["goods_code"] : '');//商品编码
        $cover_video_arr        = isset($params["cover_video_arr"]) ? $params["cover_video_arr"] : [];//封面缩略图（数组）
        $brand_id               = isset($params["brand_id"]) ? $params["brand_id"] : 0;//品牌id
        $unit_measurement       = strval(isset($params["unit_measurement"]) ? $params["unit_measurement"] : '');//计量单位
        $sort                   = intval(isset($params["sort"]) ? $params["sort"] : 100);//排序编号
        $parameter              = isset($params["parameter"]) ? $params["parameter"] : [];//商品参数
        $specifications         = isset($params["specifications"]) ? $params["specifications"] : [];//商品规格

        $single_item            = isset($params["single_item"]) ? $params["single_item"] : [];//单品数组

        $is_single_specification    = isset($params["is_single_specification"]) ? $params["is_single_specification"] : 0;//是否多规格 0否 1是
        $details_arr                = isset($params["details_arr"]) ? $params["details_arr"] : [];//商品详情图
        
        
        //数据验证
        $Validate = validate('Goods', 'validate');
        if( !$Validate -> scene('goods_edit') -> check($params) ){
            json_out(-1001, $Validate -> getError(), (object)[]);//验证失败
        }
        
        //逻辑处理
        $list = controller('Goodslogic', 'logic') 
        -> goods_edit($goods_id, $user_id, $goods_name, $goods_code, $brand_id, $supplier_id, $goods_classid, $cover_video_arr, $unit_measurement, $parameter, $single_item, $is_single_specification, $specifications, $details_arr, $goods_classtempid);

        //数据返回
        json_out($list['code'], $list['message'], (object)$list['data']);        
    }

    /**
     * 商品删除
     * @author jason
     * @return mixed
     */
    public function goods_delete(){
		//参数接收
        $params = $this->getParams(1);//0 get 1 post 
        $goods_id    = intval(isset($params["goods_id"]) ? $params["goods_id"] : 0);
        $user_id    = intval(isset($params["user_id"]) ? $params["user_id"] : 0);
        //数据验证
        $Validate = validate('Goods', 'validate');
        if( !$Validate -> scene('goods_delete') -> check($params) ){
            json_out(-1001, $Validate -> getError(), []);//验证失败
        }

        //逻辑处理
        $list = controller('Goodslogic', 'logic') -> goods_delete($goods_id, $user_id);

        //数据返回
        if($list['code'] == 200){
            json_out($list['code'], '删除成功', $list['data']);        
        }else{
            json_out($list['code'], $list['message'], $list['data']);        
        }
        
    }
	
}

